package com.atguigu.lease.web.admin.controller;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.AttrKey;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.web.admin.service.AttrKeyService;
import com.atguigu.lease.web.admin.service.AttrValueService;
import com.atguigu.lease.web.admin.vo.attr.AttrKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 李壮
 * @CreateTime: 2025-05-06
 * @Version: 1.0
 */

@RestController
@RequestMapping("/admin/attr")
@Tag(name = "房间属性管理")
public class AttrController {

    @Autowired
    AttrKeyService attrKeyService;

    @Autowired
    AttrValueService attrValueService;

    @Operation(summary = "新增或更新属性名称")
    @PostMapping("/key/saveOrUpdate")
    public Result saveOrUpdateAttrKey(@RequestBody AttrKey attrKey){
        attrKeyService.saveOrUpdate(attrKey);
        return Result.ok();
    }



    @Operation(summary = "新增或更新属性值")
    @PostMapping("/value/saveOrUpdate")
    public Result saveOrUpdateAttrValue(@RequestBody AttrValue attrValue){
        attrValueService.saveOrUpdate(attrValue);
        return Result.ok();
    }


    @Operation(summary = "查询全部属性名称和属性值列表")
    @GetMapping("/list")
    public Result listAttr(){
        List<AttrKeyVo> attrKeyVoList = attrKeyService.getAttrInfo();
        return Result.ok(attrKeyVoList);
    }


    @Operation(summary = "根据id删除属性值")
    @DeleteMapping("/value/deleteById")
    public Result deleteValueById(@RequestParam Long id){

        attrValueService.removeById(id);
        return Result.ok();
    }


    @Operation(summary = "根据id删除属性名称")
    @DeleteMapping("/key/deleteById")
    @Transactional
    public Result deleteKeyById(@RequestParam Long id){

        attrKeyService.removeById(id);

        LambdaQueryWrapper<AttrValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AttrValue::getAttrKeyId,id);
        attrValueService.remove(lambdaQueryWrapper);
        return Result.ok();
    }

}
